/**
 * Created by Garry on 2017/10/23.
 */

const log4js = require('log4js');

class Logger {
    constructor(appName) {
        this.appName = appName;

        log4js.configure({
            appenders: {
                out: {
                    type: 'stdout'
                },
                app: {
                    type: 'dateFile',
                    filename: `${appName}.log`,
                    pattern: '.yyyy-MM-dd',
                    layout: {
                        type: 'pattern',
                        pattern: '%d{yyyy-MM-ddThh:mm:ss.SSSO} %p %c - [%m]'
                    }
                }
            },
            categories: {
                default: {
                    appenders: ['out','app'],
                    level: 'debug'
                }
            },
            pm2: true,
            disableClustering: true
            // pm2InstanceVar: 'INSTANCE_ID',
        });
    }

    getLogger() {
        return log4js.getLogger(this.appName);
    }
}

module.exports = {
    getLogger: appName => {
        const key = `__boluome_global_logger_instance_${appName}`;

        if (!global[key]) {
            global[key] = new Logger(appName);
        }

        return global[key].getLogger();
    }
};


